
// 注意：这是一个vuex@3 的 module 对象，只能应用在Vue2项目中
import { postGoodsInfo } from '@/api/Search'

const state = {
    goodsInfo: {}
}
const mutations = {
    UPDATE_GOODSINFO(state, goodsInfo) {
        state.goodsInfo = goodsInfo
    }
}
const actions = {
    async postGoodsInfo({ commit }, data) {
        // 发送请求商品信息
        const res = await postGoodsInfo(data)
        if (res.code === 200) {
            commit('UPDATE_GOODSINFO', res.data)
        }
    }
}
const getters = {
    attrsList(state) {
        // 当读取不到数据时，赋值为一个空数组 避免组件使用数据时，从undefined身上读取数据报错
        return state.goodsInfo.attrsList || []
    },
    goodsList(state) {
        return state.goodsInfo.goodsList || []
    },
    trademarkList(state) {
        return state.goodsInfo.trademarkList || []
    }
}

export default {
    state,
    mutations,
    actions,
    getters,
}
